package de.gee.erep.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;

import de.gee.erep.client.ui.presenter.CountryPresenter;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class AnotherErepublikTool implements EntryPoint, ValueChangeHandler<String> {
	/***/
	private final HandlerManager eventBus = new HandlerManager(this);
	/***/
	private Environment environment;

	/**
	 * This is the entry point method.
	 */
	public final void onModuleLoad() {
		final Button sendButton = new Button("Update");
		sendButton.setSize("100px", "50px");
		sendButton.addStyleName("sendButton");
		HorizontalPanel hp = new HorizontalPanel();
		hp.setSize("200px", "50px");
		hp.add(sendButton);
		RootPanel.get("nav").add(hp);
		sendButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(final ClickEvent event) {
				environment.launch(CountryPresenter.PLACE+"&Update", RootPanel.get("con"));
			}
		});
	    setEnvironment(new Environment(new Model(), History.getToken()));
	    /*
	     * Set up the history management, and start by showing the login form.
	     */
	    History.addValueChangeHandler(this);
	    environment.launch(CountryPresenter.PLACE, RootPanel.get("con"));
	}

	/**
	 * @return the eventBus
	 */
	public final HandlerManager getEventBus() {
		return eventBus;
	}

	/* (non-Javadoc)
	 * @see com.google.gwt.event.logical.shared.ValueChangeHandler#onValueChange(com.google.
	 * gwt.event.logical.shared.ValueChangeEvent)
	 */
	@Override
	public final void onValueChange(final ValueChangeEvent<String> event) {
		environment.launch(event.getValue());
	}

	/**
	 * @param environment the environment to set
	 */
	public final void setEnvironment(final Environment environment) {
		this.environment = environment;
	}

	/**
	 * @return the environment
	 */
	public final Environment getEnvironment() {
		return environment;
	}
}
